<?PHP
	/************************************************************************
	*       This file, in addition to all other parts of X7 Chat are        *
	*                   Copyright 2003-2007 by Tim Chamness.                *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* Version:          X7 Chat 3.0.0-A2                                    *
	* Release Date:     October 20, 2007                                    *
	* Author:           Tim Chamness                                        *
	* Copyright:        2003-2007 by Tim Chamness                           *
	* Website:          http://www.x7chat.com/                              *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* X7 Chat 3 is free software; you can redistribute it and/or modify     *
	* it under the terms of the GNU General Public License as published by  *
	* the Free Software Foundation; either version 3 of the License, or     *
	* (at your option) any later version.                                   *
	*                                                                       *
	* X7 Chat 3 is distributed in the hope that it will be useful, but      *
	* WITHOUT ANY WARRANTY; without even the implied warranty of            *
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
	* General Public License for more details.                              *
	*                                                                       *
	* You should have received a copy of the GNU General Public License     *
	* along with this program.  If not, see <http://www.gnu.org/licenses/>. *
	*                                                                       *
	* Additional Terms, compatible with Section 7 of the GNU GPL v3,        *
	* apply to X7 Chat 3 and all X7 Chat 3 files, including this one.       *
	* You should have received a copy of these Additional Terms in the      *
	* file docs/COPYING.ADDITIONAL_TERMS.  If you did not receive a copy    *
	* please visit <http://www.x7chat.com/COPYING.ADDITIONAL_TERMS/>.       *
	************************************************************************/

	$GLOBALS['OUTPUT']->master_section = 'management';
	$GLOBALS['OUTPUT']->minor_section = $GLOBALS['LANG']->lang_admin->managebans;
	$GLOBALS['OUTPUT']->cpmenu = $menus[$GLOBALS['OUTPUT']->master_section];
		
	if(isset($_GET['delete'])){
		$del = x7in::read('delete','i');
		unset($settings['bans'][$del]);
		$settings['key'] = 'settings';
		$GLOBALS['DATABASE']->put('system',$settings);
		$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->bandeleted;
	}
	
	$form = new form();
	
	$form->add_field('select','bantype',$GLOBALS['LANG']->lang_admin->bantype,'',array($GLOBALS['LANG']->lang_admin->username=>'user',$GLOBALS['LANG']->lang_admin->ip=>'ip'),true);
	$form->add_field('text','banvalue',$GLOBALS['LANG']->lang_admin->banvalue);
	$form->add_field('text','banlength',$GLOBALS['LANG']->lang_admin->banlength);
	$form->add_field('select','banmultiplier',$GLOBALS['LANG']->lang_admin->banmultiplier,'',array($GLOBALS['LANG']->lang_admin->hours=>3600,$GLOBALS['LANG']->lang_admin->days=>24*3600,$GLOBALS['LANG']->lang_admin->forever=>0),true);
	
	$type = x7in::read('bantype');
	$banvalue = x7in::read('banvalue');
	if(!empty($type) && !empty($banvalue)){
		$length = x7in::read('banlength','i');
		$mult = x7in::read('banmultiplier','i');
		
		if($type == 'ip'){
			if(!preg_match("#[0-9\*]{1,3}\.[0-9\*]{1,3}\.[0-9\*]{1,3}\.[0-9\*]{1,3}#",$banvalue)){
				$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->invalidip;
			}else{
				$settings['bans'][] = array('end'=>$mult == 0 ? 0 : $ses->time+$length*$mult,'ip'=>$banvalue);
				$settings['key'] = 'settings';
				$GLOBALS['DATABASE']->put('system',$settings);
			}
		}else{
			$user = new lib_user($banvalue);
			
			if(!$user->is_valid()){
				$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->user_not_found;
			}else{
				$settings['bans'][] = array('end'=>$mult == 0 ? 0 : $ses->time+$length*$mult,'name'=>$user->data_cache['name']);
				$settings['key'] = 'settings';
				$GLOBALS['DATABASE']->put('system',$settings);
			}
		}
	}
	
	$pager = new lib_pager(count($settings['bans']));
	
	$GLOBALS['OUTPUT']->pages = $pager->get_pages();
	$GLOBALS['OUTPUT']->page = $pager->get_page();
	$GLOBALS['OUTPUT']->bans = $pager->get_page_data($settings['bans']);
	$GLOBALS['OUTPUT']->form = $form->get_fields();
	
	echo tpl_managebans::managebans();
?>